/**
 * Project Name:mauto-web
 * File Name:ApiEnquireController.java
 * Package Name:com.inftech.mauto.core.web.front
 * Date:2016年10月17日下午9:59:12
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.core.web.front;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.constant.SessionConstEnum;
import com.inftech.mauto.core.entity.Car;
import com.inftech.mauto.core.service.CarService;
import com.mingsoft.weixin.entity.WeixinPeopleEntity;

/**
 * ClassName:ApiCarController <br/>
 * Function: 车辆接口. <br/>
 * Date: 2016年10月17日 下午9:59:12 <br/>
 * 
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/api/car")
public class ApiCarController extends BaseController {

	/**
	 * 注入车辆业务层
	 */
	@Autowired
	protected CarService carService;

	/**
	 * 
	 * save:新增车辆. <br/>
	 *
	 * @author puyf
	 * @param cdAccount
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String save(@RequestBody Car car, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取当前用户信息
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			int peopleId = people.getPeopleId();
			car.setPeopleId(peopleId);
			carService.saveOrUpdateCar(car);
			LOG.info("绑定车辆成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("绑定车辆失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * detail:我的车辆详情. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/detail", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String detail(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取当前用户信息
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			int peopleId = people.getPeopleId();
			Map<String, Object> carMap = carService.queryCar(peopleId);
			LOG.info("获取车辆详情成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), carMap);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("获取车辆详情成功", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * updateMileage:(更新行驶里程). <br/>
	 *
	 * @author Administrator
	 * @param carMileage
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "updateMileage", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateMileage(@RequestParam(value = "carMileage") String carMileage, HttpServletRequest request)
			throws Exception {
		String outJson = "";
		try {
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Car car = new Car();
			car.setPeopleId(people.getPeopleId());
			car.setCarMileage(Integer.parseInt(carMileage));
			carService.saveOrUpdateCar(car);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("绑定车辆失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}
}
